package waal.pc.frontend.tiles;

import javax.swing.JPanel;

import waal.pc.backend.hub.Hub;
import waal.pc.backend.hub.HubEvent;
import waal.pc.backend.hub.HubEventListener;

public abstract class Tile extends JPanel implements HubEventListener {

	
	private static final long serialVersionUID = 5316833770695626553L;
	boolean active = false;
	int id = 0;
	String name;
	
	Hub hub;
	
	public Tile(String name) {
		this.name = name;	
	}
	
	public abstract void closeTile();
	protected abstract void hubStart();
	protected abstract void hubStop();
	public abstract boolean isDigital();
	public abstract boolean isInput();
	public abstract boolean isOutput();
	public abstract boolean isUpdateAble();
	public abstract boolean moduleTile();
	public abstract void setDimension(int width, int heigh);
	
	protected void initTile(int id, Hub hub) {
		this.id = id;
		this.hub = hub;
		this.hub.addHubEventListern(this);
	}
	
	public String toString() {
		return this.name;
	}
	
	public int getID() {
		return this.id;
	}
	
	@Override
	public void actionHubEvent(HubEvent e) {
		if(e.getEvent() == 3) { // Hub start
			if(e.getHub().equals(this.hub)) {

				this.active = true;
				this.hubStart();
			}
		} else if(e.getEvent() == 4) {// Hub stop
			
			if(e.getHub().equals(this.hub)) {

				this.active = false;
				this.hubStop();
			}
		} else if(e.getEvent() == 2) {// Hub remove
			if(e.getHub().equals(this.hub)) {
				this.closeTile();
			}
		}
	}
	
}
